﻿using Prism.Services.Dialogs;
using SharpDX;
using SmartPlant.Core;
using SmartPlant.Core.Mvvm;
using SmartPlant.Model;
using SmartPlant.Model.DbContext;
using SmartPlant.Services;
using SmartPlant.Services.Interfaces;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity;

namespace SmartPlant.Modules.Base.ViewModels
{
    public class FormSelectViewModel : DialogViewModelBase, IDialogAware
    {
        public ISpecFormService formServ { get; set; }
        #region binding


        private int _selectProcFunc;
        /// <summary>
        /// 当前仪表的工艺大类
        /// </summary>
        public int selectProcFunc
        {
            get { return _selectProcFunc; }
            set
            {
                SetProperty(ref _selectProcFunc, value);
            }
        }

        private ObservableCollection<SpecForm> _FormList;
        /// <summary>
        /// 所有的form
        /// </summary>
        public ObservableCollection<SpecForm> FormList
        {
            get { return _FormList; }
            set
            {
                SetProperty(ref _FormList, value);
            }
        }
        private SpecForm _selectForm;

        /// <summary>
        /// 选择的form
        /// </summary>
        public SpecForm selectForm
        {
            get { return _selectForm; }
            set
            {
                SetProperty(ref _selectForm, value);
            }
        }
        #endregion
        public FormSelectViewModel(IDialogService dialogService, IUnityContainer unityContainer) : base(dialogService)
        {
            formServ = unityContainer.Resolve<SpecFormService>();
        }


        /// <summary>
        /// 打开对话框，传入对话框参数。
        /// </summary>
        /// <param name="parameters"></param>
        public override async void OnDialogOpened(IDialogParameters parameters)
        {
            Title = "选择规格书模板Form";
            selectProcFunc = parameters.GetValue<int>(DialogParameterKeys.Id);
            FormList = new ObservableCollection<SpecForm>();

            var res = await formServ.GetList(select: "SPEC_FORM_ID SpecFormId,SPEC_FORM_CNUM SpecFormCnum,SPEC_NAME SpecName,PROC_FUNC_ID ProcFuncId");
            FormList.AddRange(res.Where(x => x.ProcFuncId == selectProcFunc || x.ProcFuncId ==(int)ProcFunc.general));
        }

        /// <summary>
        /// 对应 CloseDialogCommand 里的委托（在base对象上）
        /// </summary>
        /// <param name="parameter"></param>
        protected override void onCloseDialogCommand(string parameter)
        {
            if (parameter == "OK")
            {
                if (selectForm == null)
                {
                    Dialog.ShowMessage("请选择一个form。");
                    return;
                }
                else
                {
                    var res = new DialogParameters();
                    res.Add(DialogParameterKeys.Id, selectForm.SpecFormId);
                    RaiseRequestClose(new DialogResult(ButtonResult.OK, res));
                }
            }
            else if (parameter == "Cancel")
            {
                RaiseRequestClose(new DialogResult(ButtonResult.Cancel));
            }
        }


    }
}
